#include "CTurret.h"
#include "..\CCyberSpaceState.h"
#include "..\Wrappers\CSGD_Direct3D.h"
#include "..\Messaging\CSGP_MessageSystem.h"
#include "..\Wrappers\CSGD_TextureManager.h"
#include "CPistolBullet.h"
#include "..\SGD_Math.h"

CTurret::CTurret(void)
{
	SetWidth(64);
	SetHeight(64);
	SetType(CBase::OBJ_TURRET);

	m_fFireTimer = 0.0f;

	imageid = CSGD_TextureManager::GetInstance()->LoadTexture("Resource\\Graphics\\SGD_turret_edit.png");
	rad = 0.0f;

	gunpoint.fX = GetPosX() + 32;
	gunpoint.fY = (GetPosY() + 32) - 107;
	//m_tStartPos.fX = 0.0f;
	//m_tStartPos.fY = 0.0f;
}

CTurret::~CTurret(void)
{

}

//void CTurret::SetStartVector(float fPosX, float fPosY)
//{
//	m_tStartPos.fX = fPosX;
//	m_tStartPos.fY = fPosY;
//}
//
//void CTurret::SetPlayerVector(float fPosX, float fPosY)
//{
//	m_tToPlayer.fX = fPosX;
//	m_tToPlayer.fY = fPosY;
//}


void CTurret::Fire(void)
{
	CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreatePlayerBulletMessage(this, CPistolBullet::BLT_ENEMY));
}

void CTurret::Update(float fElapsedTime)
{
	CEnemy::Update(fElapsedTime);

	m_fFireTimer += fElapsedTime;

	// Set start position vector
	//m_tStartPos.fX = GetPosX();
	//m_tStartPos.fY = GetPosY();

	if(m_fFireTimer > 0.5)
	{
		Fire();
		m_fFireTimer = 0.0f;
	}
	tVector2D hard;
	hard.fX = 0;
	hard.fY = 1;
	tVector2D it;
	tVector2D player;
	player.fX = CPlayer::GetInstance()->GetPosX();
	player.fY = CPlayer::GetInstance()->GetPosY();
	tVector2D turret;
	turret.fX = GetPosX();
	turret.fY = GetPosY();
		it.fX = turret.fX - player.fX;
		it.fY = turret.fY - player.fY;

	rad = AngleBetweenVectors(it, hard);
	if(CPlayer::GetInstance()->GetPosX() <= GetPosX())
	{
		rad = -rad;
	}

	//m_tToPlayer.fX = CCyberSpaceState::GetInstance()->GetPlayer()->GetPosX() + 
	//	CCyberSpaceState::GetInstance()->GetPlayer()->GetWidth()*0.5f;
	//m_tToPlayer.fY = CCyberSpaceState::GetInstance()->GetPlayer()->GetPosY() + 
	//	CCyberSpaceState::GetInstance()->GetPlayer()->GetHeight()*0.5f;
}

void CTurret::Render(void)
{
	RECT drawRect = GetCollisionRect();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();
	// Show collision box
	
	

	RECT base;
	base.top = 30;
	base.left = 47;
	base.right = base.left + 64;
	base.bottom = base.top + 64;

	CSGD_TextureManager::GetInstance()->Draw(imageid, drawRect.left, drawRect.top , 1.0f, 1.0f, &base);

	RECT gun;
	gun.left = 171;
	gun.top = 3;
	gun.bottom = gun.top + 122;
	gun.right = gun.left + 63;

	CSGD_TextureManager::GetInstance()->Draw(imageid, drawRect.left + 15, drawRect.top - 50, .75f, .75f, &gun, 21, 107, rad);


	// Draw health above enemies head
	//char buffer[128];
	//sprintf_s(buffer, _countof(buffer), "HP: %i", GetHealth());

	//CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)GetPosX(), (int)GetPosY() - 20, 255, 0, 0);
}

RECT CTurret::GetCollisionRect(void)
{
	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

bool CTurret::CheckCollision(IBaseInterface* IBase)
{
	if(CEnemy::CheckCollision(IBase))
		return true;

	return false;
}

void CTurret::HandleEvent(CEvent* pEvent)
{

}